'use strict';
var project = require('../conf.js');
var request = require('/request.js');
var modal = require('/modal.js');
module.exports = {
  callPay:function(payObj){
    var isbeta = (project.domain.indexOf('betamina') != -1);//是否为beta环境
    var paySignCgi = project.paySignCgi;
    // data为获取paySign时需要传的参数
    var data = {};
    data.order_id = payObj.order_id;
    data.wx_session = payObj.wx_session;

    if(isbeta) {// 测试环境
      paySignCgi = project.domain + '/wxapp_api/api'
      data.action = 'wxapp_api/pay/get_jsapi_config'
    }
    wx.request({
      url: paySignCgi,
      method: 'POST',
      data: data,
      header: {
          'Content-Type': 'application/json'
      },
      success:function(res){
        var resData = res.data.data;
        wx.requestPayment({
          'timeStamp': resData.timeStamp,
          'nonceStr': resData.nonceStr,
          'package': resData.package,
          'signType': 'MD5',
          'paySign': resData.paySign,
          'success':function(res){
            payObj.success(res);
          },
          'fail':function(res){
            payObj.fail(res)
          }
        });
      },
      fail:function(res){
        modal.confirm('',res.errMsg,function(){})
      }
    })
  }
}